package com.ruoyi.basebill.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basebill.domain.OmsSaleissuebill;
import com.ruoyi.basebill.domain.OmsSalereturnbill;
import com.ruoyi.basebill.service.IOmsDeliverbillService;
import com.ruoyi.basebill.service.IOmsSaleissuebillService;
import com.ruoyi.basebill.service.IOmsSalereturnbillService;
import com.ruoyi.basebill.util.OmsUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basebill.domain.OmsTransferbill;
import com.ruoyi.basebill.service.IOmsTransferbillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * OMS调拨单Controller
 *
 * @author ptt
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/settlementCenter/basebill/transferbill")
public class OmsTransferbillController extends BaseController
{
    @Autowired
    private IOmsTransferbillService omsTransferbillService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private OmsUtil omsUtil;

    /**
     * 查询OMS调拨单列表
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsTransferbill omsTransferbill)
    {
        startPage();
        List<OmsTransferbill> list = omsTransferbillService.selectOmsTransferbillList(omsTransferbill);
        return getDataTable(list);
    }

    /**
     * 导出OMS调拨单列表
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:export')")
    @Log(title = "OMS调拨单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmsTransferbill omsTransferbill)
    {
        List<OmsTransferbill> list = omsTransferbillService.selectOmsTransferbillDetailList(omsTransferbill);
        for (OmsTransferbill transferbill : list) {
            String ordertype = transferbill.getOrdertype();
            String transfer_type = sysDictDataService.selectDictLabel("transfer_type", ordertype);
            transferbill.setOrdertype(transfer_type);
        }
        ExcelUtil<OmsTransferbill> util = new ExcelUtil<OmsTransferbill>(OmsTransferbill.class);
        util.exportExcel(response, list, "OMS调拨单数据");
    }

    /**
     * 获取OMS调拨单详细信息
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(omsTransferbillService.selectOmsTransferbillById(id));
    }

    /**
     * 新增OMS调拨单
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:add')")
    @Log(title = "OMS调拨单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OmsTransferbill omsTransferbill)
    {
        return toAjax(omsTransferbillService.insertOmsTransferbill(omsTransferbill));
    }

    /**
     * 修改OMS调拨单
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:edit')")
    @Log(title = "OMS调拨单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmsTransferbill omsTransferbill)
    {
        return toAjax(omsTransferbillService.updateOmsTransferbill(omsTransferbill));
    }

    /**
     * 删除OMS调拨单
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:remove')")
    @Log(title = "OMS调拨单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(omsTransferbillService.deleteOmsTransferbillByIds(ids));
    }




    /**
     * 查询下游单据
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:list')")
    @GetMapping("/DownBill/{Number}/{type}")
    public AjaxResult getDownBill(@PathVariable("Number") String Number,@PathVariable("type") String type)
    {
        Map downBill = omsUtil.getDownBill(Number,type);
        return success(downBill);
    }

    /**
     * 查询下游单据详情
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:list')")
    @GetMapping("/getDownBillInfo/{id}/{type}")
    public AjaxResult getDownBillInfo(@PathVariable("id") Long id,@PathVariable("type") String type)
    {
        Map downBill = omsUtil.getDownBillInfo(id,type);
        if(downBill!=null&&downBill.get(type)!=null){
            return success(downBill.get(type));
        }
        return null;
    }

    /**
     * 删除下游单据
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:list')")
    @GetMapping("/delDownBill/{ids}/{type}")
    public AjaxResult delDownBill(@PathVariable("ids") Long[] ids,@PathVariable("type") String type)
    {
        int i = omsUtil.delDownBill(ids, type);
        return success(i);
    }

    /**
     * 回写上游单据下发状态
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:list')")
    @GetMapping("/updataUpBill/{id}/{type}")
    public AjaxResult updataUpBill(@PathVariable("id")Long id
            ,@PathVariable("type")String type)
    {
        int i = omsUtil.updataUpBill(id, type);
        return success(i);
    }

    /**
     * 单据下发
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basebill:transferbill:list')")
    @GetMapping("/handlePush/{id}/{type}")
    public AjaxResult handlePush(@PathVariable("id")Long[] id
            ,@PathVariable("type")String type)
    {
        int i = omsUtil.handlePush(id, type);
        return success(i);
    }
}
